﻿/**	auto_close.hpp
 *	----	Create Time Stamp	--2007-11-25 15:39:57--
 *------------------------------------------------------------------------------
 *	Mean	:Plantform handle
 *	Author	:Nick Shallery	(nicknide@gmail.com)
 *	Version	:1.0.4.4	(2008-02-10 01:30:01)
 *------------------------------------------------------------------------------
**/	/*YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.*/

#if			!defined(__LOLIX__TOY__AUTO_CLOSE_H__NICK__K_IN_G__)
#define		__LOLIX__TOY__AUTO_CLOSE_H__NICK__K_IN_G__


#if			!defined(__LOLIX__LOLI_X_MACRO__K_IN_G__)
	#include	"./lolix_macro.h"
#endif	//	!defined(__LOLIX__LOLI_X_MACRO__K_IN_G__)

template<typename _Ty>	class	auto_close;
template<typename, typename>	class auto_close2;
#define		LOLIX_DEF__MAKE_AUTO_CLOSE_TYPE(handle_type, close_proc)	\
template<>\
class	auto_close<handle_type>\
{\
	CANNT_COPY_LOLI;\
public:\
	LOLIX_INLINE_CALL	auto_close<handle_type>(handle_type hd)\
		: _hd(hd)\
	{\
	}\
\
	LOLIX_INLINE_CALL	~auto_close<handle_type>(void)\
	{\
		if ( _hd )	close_proc(_hd);\
	}\
\
	LOLIX_INLINE_CALL	handle_type	reset(void)\
	{\
		handle_type rtv = _hd;\
		_hd = 0;\
		return rtv;\
	}\
private:\
	handle_type	_hd;\
}


#define		LOLIX_DEF__MAKE_AUTO_CLOSE_TYPE_2(parent_type, handle_type, close_proc)	\
template<>\
class	auto_close2<parent_type, handle_type>\
{\
	CANNT_COPY_LOLI;\
public:\
	LOLIX_INLINE_CALL	auto_close2<parent_type, handle_type>(parent_type pd, handle_type hd)\
		: _pd(pd), _hd(hd)\
	{\
	}\
\
	LOLIX_INLINE_CALL	~auto_close2<parent_type, handle_type>(void)\
	{\
		if ( _hd )	close_proc(_pd, _hd);\
	}\
\
	LOLIX_INLINE_CALL	handle_type	reset(void)\
	{\
		handle_type rtv = _hd;\
		_hd = 0;\
		return rtv;\
	}\
private:\
	handle_type	_hd;\
	parent_type	_pd;\
}


//LOLIX_DEF__MAKE_AUTO_CLOSE_TYPE(::HANDLE, ::CloseHandle);
//LOLIX_DEF__MAKE_AUTO_CLOSE_TYPE(::HMODULE, ::FreeLibrary);
//LOLIX_DEF__MAKE_AUTO_CLOSE_TYPE_2(::HWND, ::HDC, ::ReleaseDC);

#endif	//	!defined(__LOLIX__TOY__AUTO_CLOSE_H__NICK__K_IN_G__)

